module FuelPricesEurope

  class FuelPrices

    def initialize
      load_data
    end

    # Find data from multiple countries. Pass in an array of countries.
    # By default you must use alpha2 codes, but you can specify :alpha3 or :name as an extra argument.
    # A hash with +CountryData+ objects will be returned with the input countries as a key.
    def find_by_countries(countries, type = :alpha2)
      result = {}
      countries.each do |country|
        if type == :alpha2
          country_data = find_by_country_alpha2(country)
        elsif type == :alpha3
          country_data = find_by_country_alpha3(country)
        else
          country_data = find_by_country_name(country)
        end
        result[country] = country_data unless country_data.nil?
      end
      result
    end

    def find_by_country_alpha2(alpha2)
      @country_data[alpha2.upcase] || nil
    end

    alias_method :find_by_country_code, :find_by_country_alpha2


    def find_by_country_name(country_name)
      load_data
      alpha2 = Country.find_country_by_name(country_name).alpha2
      @country_data[alpha2] || nil
    end


    def find_by_country_alpha3(alpha3)
      load_data
      alpha2 = Country.find_country_by_alpha3(alpha3).alpha2
      @country_data[alpha2] || nil
    end

    private

    def load_data
      @country_data = Scraper.scrape
    end

  end
end